﻿/* Nibiru Studio Engine Version [1070] */
#include "WorlsEye.h"
#include "Apis/NEngineApi.h"

ENGINE_NAMESPACE_BEGIN

IMPLEMENT_CLASS(WorlsEye)
IMPLEMENT_RTTI_CUSTOM(WorlsEye, NComponent)

//ENUM_BEGIN(LabelType, LabelType)
// add your enum
// Display Name, Enum Type Name
// ENUM(Left, LEFT)
//ENUM_END


REFLECTION_BEGIN(WorlsEye)
// add your variable reflection property
//Enum Class Name, Display Name In Editor, Variable Name In Class, flags default is 0
//ENUM_PROPERTY(LabelType, Type, m_LabelType, 0)
//PROPERTY(Bold, m_IsBold, 0)
//PROPERTY(Rotate, m_EnableRotate, 0)
//FUNCTION(OnButtonClick, WorlsEye, OnClick)

REFLECTION_END


void WorlsEye::Start()
{
	// Triggered before the first execution of Update

	//	获取模拟眼动焦点的对象
	m_sphere_start = NActorManager::GetActor("Sphere_start");
	startPoint = m_sphere_start->GetPosition(); //确定起点位置


	m_sphere_end = NActorManager::GetActor("Sphere_end");
	endPoint = m_sphere_end->GetPosition(); //确定终点位置


	file = std::ifstream(filePath); //打开射线文件
	if (!file.is_open()) {
		std::cerr << "Unable to open file: " << filePath << std::endl;
		return;
	}
}

void WorlsEye::Update()
{
	// Called every frame if actor is enabled.

	if(std::getline(file, line)) //读取每一行
	{
		std::istringstream iss(line);
		std::string cell;
		std::vector<float> row;
		while (std::getline(iss, cell, ',')) {
			// 在这里可以添加代码来处理或清理单元格内容
			row.push_back(std::stof(cell));
		}
		dirPoint = {row[0], -row[1], -row[2]}; //获取眼动方向
		NDebug::Log("======read frame data=====\n");


		endPoint = startPoint + dirPoint * 0.4f;
		m_sphere_end->SetPosition(endPoint); //设置局部坐标系的位置

		NGraphics::DrawLine(startPoint, endPoint, Color::Red);


	}else
	{
		file.close();
		NDebug::Log("======read frame data end======\n");

		file = std::ifstream(filePath); //打开射线文件
		if (!file.is_open()) {
			std::cerr << "Unable to open file: " << std::endl;
			return;
		}


	}

//	退出键退出应用
	if(NInput::GetKeyDown(KeyboardKeys::Android_Back))
	{
		NApplication::Quit();
	}
}

void WorlsEye::OnDestroy()
{
	// When the component deleted
}

void WorlsEye::PostEditChangeProperty(std::string& propertyName)
{
	// Variable Property changed in Editor
}

void WorlsEye::Serialize(Archive& ar)
{
	Super::Serialize(ar);
	// SERIALIZE(m_EnableRotate);
}

void WorlsEye::LateDeserialize(Archive& ar)
{
	// After all Serialize are executed
	Super::LateDeserialize(ar);
}

ENGINE_NAMESPACE_END
